<template>
  <el-form ref="userRef" :model="form.data" :rules="rules" label-width="100px">
    <el-form-item label="手机号码：" prop="phonenumber">
      <el-input v-model="form.data.phonenumber" maxlength="11" />
    </el-form-item>
    <el-form-item label="用户邮箱：" prop="email">
      <el-input v-model="form.data.email" maxlength="50" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleSubmit">保存</el-button>
    </el-form-item>
  </el-form>
</template>
<script setup>
import { reactive, ref } from "vue";
import requestUtil from "@/util/request";
import { ElMessage } from "element-plus";
import store from "@/store";

const props = defineProps({
  user: {
    type: Object,
    default: () => {},
    require: true,
  },
});

const userRef = ref(null);
const form = reactive({
  data: {
    phonenumber: "",
    email: "",
  },
});

const rules = reactive({
  email: [
    { required: true, message: "邮箱地址不能为空", trigger: "blur" },
    {
      type: "email",
      message: "请输入正确的邮箱地址",
      trigger: ["blur", "change"],
    },
  ],
  phonenumber: [
    { required: true, message: "手机号码不能为空", trigger: "blur" },
    {
      pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
      message: "请输入正确的手机号码",
      trigger: "blur",
    },
  ],
});
//props 传递参数
form.data = props.user;

//提交修改按钮 传入后端数据
let handleSubmit = () => {
  userRef.value.validate(async (valid) => {
    let result = await requestUtil.post("user/save", form.data);
    if (result.code == 20000) {
      ElMessage.success("修改成功");
      store.commit("SET_USER", form.data);
    }
  });
};
</script>
<style lang="scss" scoped>
</style>